﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using digitwhu.Models.IRepositorys;
using digitwhu.Models;


namespace digitwhu.Models.Repositorys
{
    public class CanteenRepository : Repository<Canteen>, ICanteenRepository
    {
        //食堂相关的数据访问和数据更新
        public CanteenRepository(DataContext dbContext)
            : base(dbContext)
        {
            //构造函数
        }
        public int GetCanteenIdByBuildingId(int buildingId)//根据给定的建筑物id，得到自定义食堂编号
        {
            //buildingId:建筑物ID
            //返回值：食堂自定义编号,负数(-1)表示参数错误
            var b = from c in context.GetTable<Canteen>()
                    where c.buildingId == buildingId
                    select c.id;
            foreach (int i in b)
            {
                return i;
            }
            return -1;
        }
        public List<int> GetCanWindowList(int canteenId) //获取某个食堂的所有窗口列表
        {
            //canteenId:预获取所有窗口的食堂的编号
            //返回值：食堂窗口的编号列表
            var window = from w in context.GetTable<CanShopWindow>()
                         where w.canteenId == canteenId
                         select w.id;
            List<int> result = new List<int>();
            foreach (int i in window)
            {
                result.Add(i);
            }
            return result;
        }
        public List<int> GetCanWindowGrade(int canWindowId)//获取某个食堂窗口的评分列表
        {
            //canWindowId:预获得评分的窗口编号
            //返回值：某个窗口的评分列表
            List<int> result = new List<int>();
            var grade = from g in context.GetTable<CanGradeRecord>()
                        where g.shopWindowId == canWindowId
                        select g.grade;
            foreach (int i in grade)
            {
                result.Add(i);
            }
            return result;
        }
        public bool AddCanWindowGrade(int userId, int canWindowId, int grade)  //给某个窗口评分
        {
            //userId:要给窗口评分的用户编号
            //canWindowId:要评分的窗口编号
            //grade:用户给的分数(只能是0~5)
            //返回值：true:用户评分成功   false:用户评分失败
            CanGradeRecord record = new CanGradeRecord();
            record.dateTime = DateTime.Now;
            record.grade = byte.Parse(grade.ToString());    //注意转换
            record.userId = userId;
            record.shopWindowId = canWindowId;
            try
            {
                context.GetTable<CanGradeRecord>().InsertOnSubmit(record);
                context.SubmitChanges();
                return true;
            }
            catch (Exception e)
            {
                //操作失败，可能是用户输入的参数非法
                return false;
            }
        }
        public List<CanMessage> GetCanMessageList(int canteenId)//获取某个食堂的所有留言
        {
            //canteenId:预获取留言的食堂编号
            //返回值：某个食堂的所有留言对象列表
            List<CanMessage> result = new List<CanMessage>();
            var message = from m in context.GetTable<CanMessage>()
                          where m.canteenId == canteenId
                          select m;
            foreach (var m in message)
            {
                result.Add(m);
            }
            return result;
        }
        public bool AddCanMessage(int userId, int canteenId, string content)//给某个食堂留言
        {
            //userId:待发表留言的用户编号
            //canteenId:预被留言的食堂编号
            //content:留言内容
            //返回值：操作是否成功。true:发表留言成功    false:发表留言失败
            CanMessage message = new CanMessage();
            message.userId = userId;
            message.canteenId = canteenId;
            message.contents = content;
            message.dateTime = DateTime.Now;
            try
            {
                context.GetTable<CanMessage>().InsertOnSubmit(message);
                context.SubmitChanges();        //更新到数据表
                return true;
            }
            catch (Exception e) //发生异常，说明给定参数非法
            {
                return false;       //发表留言失败
            }
        }
        public CanMessage GetLatestCanMessage(int canteenId)//获得最新的食堂留言
        {
            //canteenId:食堂编号
            //返回值：null或者最新的CanMessage对象
            var message = from m in context.GetTable<CanMessage>()
                          where m.canteenId == canteenId
                          select m;
            if (message.Count<CanMessage>() < 1)
            {
                return null;    //返回空值
            }
            CanMessage result = message.First<CanMessage>();
            foreach (CanMessage m in message)
            {
                if (DateTime.Compare(m.dateTime, result.dateTime) > 0)//r1早于r2,r1<r2
                {
                    result = m;
                }
            }
            return result;

        }
        public List<int> GetCanRecommendList(int canteenId)//获取给定食堂的推荐美食列表
        {
            //canteenId:预获取食堂推荐美食的食堂编号
            //返回值：推荐美食记录的编号列表
            var recommend = from r in context.GetTable<CanRecommendFood>()
                            where r.canteenId == canteenId
                            select r.id;
            List<int> result = new List<int>();     //返回结果集
            foreach (int i in recommend)
            {
                result.Add(i);
            }
            return result;
        }
        public bool AddCanRecommendFood(int userId, int canteenId, string contents, string picture)//给食堂推荐食物
        {
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：true：推荐美食成功 false:推荐美食失败
            try
            {
                CanRecommendFood recommend = new CanRecommendFood();
                recommend.userId = userId;
                recommend.canteenId = canteenId;
                recommend.contents = contents;
                recommend.dateTime = DateTime.Now;
                recommend.picture = picture;
                context.GetTable<CanRecommendFood>().InsertOnSubmit(recommend);
                context.SubmitChanges();        //保存更改可能出现异常
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public int GetLatestCanRecommendFoodId(int canteenId)//获取指定食堂最新的推荐美食
        {
            //canteenId:预获得最新推荐美食的食堂编号
            //返回值：正数表明最新的推荐美食记录编号，负数表示系统出现异常
            //说明：推荐美食id是自动增长的，所以最大的id编号应该是最新的美食推荐记录
            int result = -1;
            var recommend = from r in context.GetTable<CanRecommendFood>()
                            select r.id;
            foreach (int r in recommend)
            {
                if (r > result)
                {
                    result = r;
                }
            }
            return result;          //返回最新的推荐美食记录id
        }
        public bool DeleteCanRecommendFood(int recommendId)//删除自己曾经推荐的美食记录
        {
            //recommendId:将要删除的推荐美食记录编号
            //返回值：true：删除成功 false:删除失败
            var recommend = from r in context.GetTable<CanRecommendFood>()
                            where r.id == recommendId
                            select r;
            foreach (var r in recommend)
            {
                //应该只有一条记录
                try
                {
                    context.GetTable<CanRecommendFood>().DeleteOnSubmit(r);
                    context.SubmitChanges();
                    return true;
                }
                catch (Exception e)
                {
                    return false;   //数据库更新异常
                }
            }
            return false;       //要删除的记录不存在

        }
        public bool UpdateCanRecommendFood(int recommendId, string contents, string pictrue)//更新用户推荐的美食记录
        {
            //recommendId:推荐美食的ID
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：true：更改推荐美食成功 false:更改推荐美食失败
            try
            {
                //获得记录
                CanRecommendFood recommend = context.GetTable<CanRecommendFood>().Single<CanRecommendFood>(c=>c.id==recommendId );
                recommend.contents = contents;
                recommend.picture = pictrue;
                context.SubmitChanges();        //保存更改可能出现异常
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}